$(function() {
	layui.use(['form', 'table', 'laypage', 'layer', 'laydate'], function (form, table, laypage, layer, laydate) {

		var searchParam = {
			req: {
				staffId:'',
				code:'',
				state:'',
				opetatorName:'',
				email:'',
				phoneNumber:''
			},
			page: 1,
			limit: 10,

		};

		var loadData = function (page) {
			searchParam.page = CoreUtil.isEmpty(page) ? searchParam.page : page;
			//table数据加载
			CoreUtil.sendAjax("/operator/getOperatorInfo", JSON.stringify(searchParam), function (res) {
				if (res.data && res.data.records != null) {
					layer.msg('查询成功', {icon: 1});
					loadTable(res.data.records);
					laypageTable(res.data.total);
				} else {
					layer.msg('查询失败', {icon: 2});
				}
			}, "POST", false, function (res) {
				layer.msg("抱歉！您暂无获取列表的权限");
				var noAuthorityData = [];
				loadTable(noAuthorityData);
			});
		};

		//日期加载
		laydate.render({
			format: "yyyy-MM-dd HH:mm:ss",
			type: 'datetime',
			elem: 'input[name="startTime"]'
		});

		laydate.render({
			format: "yyyy-MM-dd HH:mm:ss",
			type: 'datetime',
			elem: 'input[name="endTime"]'
		});

		var loadTable = function (data) {
			table.render({
				elem: '#operator_table',
				cols: [[{type: 'checkbox', width: 50},

					{
						field: 'operatorId',
						title: '主键',
						minWidth: 120
					}, {
						field: 'staffId',
						title: '操作者编号',
						minWidth: 120
					}, {
						field: 'code',
						title: '登录名',
						minWidth: 120
					}, {
						field: 'opetatorName',
						title: '员工姓名',
						minWidth: 120
					}, {
						field: 'password',
						title: '密码',
						minWidth: 120
					}, {
						field: 'state',
						title: '状态',
						templet: function (field) {
							return field.state == '1' ? "生效" : "失效";
						},
						minWidth: 60
					}, {
						field: 'createTime',
						title: '创建时间',
						width: 180
					}, {
						field: 'recentPassword',
						title: '初始密码',
						width: 120
					}, {
						field: 'organization',
						title: '组织全称',
						width: 360
					}, {
						field: 'email',
						title: '邮箱',
						width: 180
					}, {
						field: 'phoneNumber',
						title: '手机号码',
						width: 180
					}, {
						field: 'cmosModifyTime',
						title: '修改时间',
						width: 180
					}, {
						field: 'operatorRole',
						title: '角色',
						width: 180
					}]],
				data: data,
				even: true,
				limit: data.length,
				limits: [10, 20, 30, 40, 50],
				toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
				//defaultToolbar: ['filter', 'exports', 'print'] ///这里在右边显示
			});
		};

		var laypageTable = function (total) {
			laypage.render({
				elem: 'laypage',
				count: total,
				limit: searchParam.limit,
				curr: searchParam.page,
				layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
				jump: function (obj, first) {
					if (!first) {
						searchParam.page = obj.curr;
						searchParam.limit = obj.limit;
						loadData();
					}
				}
			});
		};
		//查询操作
		form.on('submit(sreach)', function (data) {
			searchParam.req.staffId = data.field.staffId;
			searchParam.req.phoneNumber = data.field.phoneNumber;
			searchParam.req.code = data.field.code;
			searchParam.req.email = data.field.email;
			if (data.field.state == "on"){
				searchParam.req.state = "1";
			}else {
				searchParam.req.state = "0";
			}

			searchParam.req.opetatorName = data.field.opetatorName;
			searchParam.req.staffId = data.field.staffId;
			loadData();
			return false;//表单提交不刷新,为true会刷新,默认刷新
		});


		//监听事件
		table.on('toolbar(operators)', function(obj){
			var checkStatus = table.checkStatus(obj.config.id);

			var data = checkStatus.data //获取选中行的数据
			var length = checkStatus.data.length; //获取选中行数量，可作为是否有选中行的条件
			switch(obj.event){
				case 'add':
					xadmin.open('添加用户','./member-add.html',600,450);
					break;
				case 'delete':
					if (length <= 0){
						layer.msg("至少要选择一条数据",{icon:5,time:1000});
						break;
					}
					deleteOperato(data);
					break;
				case 'update':
					if (length == 0){
						layer.msg("你没有选择数据！",{icon:5,time:1000});
						break;
					}else if (length > 1){
						layer.msg("一次只能编辑一条数据！",{icon:5,time:1000});
						break;
					}else if (length == 1){
						xadmin.open('编辑用户信息','./member-edit.html',600,450);
						$('#oneRowTableData').val(JSON.stringify(data[0]).toString());
						break;
					}
					console.log(obj.data)
					break;

			};
		});

		/*根据表格数据删除用户*/
		var deleteOperato = function(data){

			if (data == null) return;
			/*主键*/
			var ids = new Array();
			$.each(data,function (index,value){
				ids.push(value.operatorId);
			});

			//询问框

			layer.confirm('您确定要删除吗？', {
				btn: ['确定','取消'] //按钮
			}, function(){
				//加载层
				layer.load();

				CoreUtil.sendAjax("/operator/delete",JSON.stringify(ids),function (data){
					if ("0" == data.code){

						layer.msg("删除成功！",{icon:1,time:1000});

						loadData();
					}else {
						layer.msg(data.msg,{icon:5,time:1000});
					}

				}, "POST", false, function (res) {
					layer.msg("系统异常");
				});
				//关闭弹出层
				layer.closeAll('loading');
			});


		};

		loadData();

	});
})